//
//  XQCourseDetailTableHeaderView.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/29.
//  Copyright © 2016年 校企桥. All rights reserved.
//

import UIKit
import Accelerate

class XQCourseDetailTableHeaderView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    

    var backButton = UIButton()
    private var actionButton = UIButton()
    
    var bgView = UIImageView()
    
    var titlelabel = UILabel()
    var typeLabel1 = UILabel()
    var typeLabel2 = UILabel()
    var typeLabel3 = UILabel()
    
    var salaryLabel = UILabel()
    var peopleLabel = UILabel()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        InitUI()
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var homeself:XQCourseDetailViewController?


    var csMode : XQCourseDetailCSMode?{
        didSet{
            
            let url = URL.init(string: (csMode?.course_photo)!)
           // bgView.kf.setImage(with: url)
           
            let data = NSData.init(contentsOf: url!)
            let image = UIImage.init(data: data as! Data)?.blur()

            //第一种 UIImage+ImageEffects
            bgView.image = image?.blurImage(withRadius: 0.2)
            
            //第二种(不怎么好)UIVisualEffectView
            //let effectView =  UIVisualEffectView.init(frame: self.frame)
            //effectView.effect = UIBlurEffect.init(style: .light)
            //bgView.addSubview(effectView)
            
            //第三种 通用模糊效果
            //bgView.image = image?.blurryImage(image, withBlurLevel: 1)
            
            //第四种 Core Image
            //bgView.image = image?.blurryImage(image, withMaskImage: image, blurLevel: 10)
            
            
            titlelabel.text = csMode?.course_name
            if csMode?.type == "video" {
                typeLabel1.text = "视频课"
            }else if csMode?.type == "train" {
                typeLabel1.text = "培训课"
            }else{
                typeLabel1.text = "直播课"
            }
            
            typeLabel2.text = csMode?.author
            typeLabel3.text = csMode?.status
            let price = Float((csMode?.price)!)
            if price == 0 {
                salaryLabel.text = "免费"
                actionButton .setTitle("开始学习", for: .normal)
                actionButton.backgroundColor = UIColor.gray
                actionButton .addTarget(self, action: #selector(self.actionbegin), for: .touchUpInside)
            }else{
                salaryLabel.text = "¥" + (csMode?.price)!
                actionButton .setTitle("购买课程", for: .normal)
                actionButton.backgroundColor = UIColor.orange
                actionButton .addTarget(self, action: #selector(self.actionbegin), for: .touchUpInside)

            }
            if ((csMode?.study_num) != nil) {
                peopleLabel.text = "学习人数:" + (csMode?.study_num)!
            }
        }
    }
    
    @objc private func actionbegin()  {
        let price = Float((csMode?.price)!)
        if price == 0 {
            let detailVC = XQCourseVideoViewController()
            detailVC.cid  = (csMode?.id)!
            detailVC.type = (csMode?.type)!
            homeself?.navigationController?.pushViewController(detailVC, animated: true)
        }else{
        
        }
        
    }
    
  
    
    func InitUI(){
        
        bgView = UIImageView.init(frame: CGRect.zero)
        bgView.backgroundColor = UIColor.lightGray
        self.addSubview(bgView)
        
        bgView.isUserInteractionEnabled = true
        
      
        
        backButton = UIButton.init(frame: CGRect.zero)
        backButton .setImage(UIImage.init(named: "back"), for: .normal)
        bgView.addSubview(backButton)
       // backButton .addTarget(homeself, action: #selector(homeself.backLeft), for: .touchUpInside)

        
        titlelabel = UILabel.init(frame: CGRect.zero)
        titlelabel.font = UIFont.systemFont(ofSize: 20)
        titlelabel.textColor = UIColor.white
        titlelabel.numberOfLines = 0
        bgView.addSubview(titlelabel)

        typeLabel1 = UILabel.init(frame: CGRect.zero)
        typeLabel1.font = UIFont.systemFont(ofSize: 12)
        typeLabel1.layer.borderWidth = 0.5
        typeLabel1.layer.borderColor = UIColor.white.cgColor
        typeLabel1.textColor = UIColor.white
        bgView.addSubview(typeLabel1)
        
        typeLabel2 = UILabel.init(frame: CGRect.zero)
        typeLabel2.font = UIFont.systemFont(ofSize: 12)
        typeLabel2.layer.borderWidth = 0.5
        typeLabel2.layer.borderColor = UIColor.white.cgColor
        typeLabel2.textColor = UIColor.white
        bgView.addSubview(typeLabel2)
        
        typeLabel3 = UILabel.init(frame: CGRect.zero)
        typeLabel3.font = UIFont.systemFont(ofSize: 12)
        typeLabel3.layer.borderWidth = 0.5
        typeLabel3.layer.borderColor = UIColor.white.cgColor
        typeLabel3.textColor = UIColor.white
        bgView.addSubview(typeLabel3)
        
        
        actionButton = UIButton.init(frame: CGRect.zero)
        actionButton.backgroundColor = UIColor.gray
        actionButton .setTitle("开始学习", for: .normal)
        actionButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        actionButton.layer.cornerRadius = 5
        bgView.addSubview(actionButton)
        
        salaryLabel = UILabel.init(frame: CGRect.zero)
        salaryLabel.font = UIFont.systemFont(ofSize: 13)
        salaryLabel.textColor = UIColor.orange
        bgView.addSubview(salaryLabel)
        
        peopleLabel = UILabel.init(frame: CGRect.zero)
        peopleLabel.font = UIFont.systemFont(ofSize: 13)
        peopleLabel.textColor = UIColor.white
        bgView.addSubview(peopleLabel)

        bgView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.top.equalTo(self.snp.top)
            make.bottom.equalTo(self.snp.bottom)
        }
        
        backButton.snp.makeConstraints { (make) in
            make.left.equalTo(bgView.snp.left).offset(20)
            make.top.equalTo(bgView.snp.top).offset(10)
            make.width.equalTo(30)
            make.height.equalTo(30)
        }
        
        
 
        typeLabel1.snp.makeConstraints { (make) in
            make.left.equalTo(bgView.snp.left).offset(10)
            make.bottom.equalTo(bgView.snp.bottom).offset(-5)
        }
        
        typeLabel2.snp.makeConstraints { (make) in
            make.left.equalTo(typeLabel1.snp.right).offset(5)
            make.bottom.equalTo(bgView.snp.bottom).offset(-5)
        }
        
        typeLabel3.snp.makeConstraints { (make) in
            make.left.equalTo(typeLabel2.snp.right).offset(5)
            make.bottom.equalTo(bgView.snp.bottom).offset(-5)

        }
        
        actionButton.snp.makeConstraints { (make) in
            make.right.equalTo(bgView.snp.right).offset(-10)
            make.top.equalTo(bgView.snp.top).offset(10)
        }
        
        titlelabel.snp.makeConstraints { (make) in
            make.left.equalTo(backButton.snp.right).offset(5)
            make.top.equalTo(bgView.snp.top).offset(10)
            make.right.equalTo(actionButton.snp.left).offset(-20)
        }
                
        salaryLabel.snp.makeConstraints { (make) in
            make.top.equalTo(actionButton.snp.bottom).offset(5)
            make.centerX.equalTo(actionButton.snp.centerX)
        }
        
        peopleLabel.snp.makeConstraints { (make) in
            make.right.equalTo(bgView.snp.right).offset(-10)
            make.bottom.equalTo(bgView.snp.bottom).offset(-5)
        }

    }
    
    

    

}
